*** Settings ***
Documentation                   openUBMC的cli命令整合
Resource                        ../../test_function/cli/ssh.robot
Resource                        ../../test_function/resource.robot
Library                         String
Suite Setup                     Open SSH Connection
Suite Teardown                  Close SSH Session    ${ssh}

*** Variables ***
${time_out}                     15s
${input_maca}                   busctl --user tree introspect bmc.kepler.maca /bmc/kepler/maca/MicroComponent
${output_maca}                  InitCompleted
${input_bcu}                    busctl --user call bmc.kepler.hwproxy /bmc/kepler/Chip/Smc/Smc_CpuBrdSMC_010101 bmc.kepler.Chip.BlockIO Read a{ss}uu 0 0x500 2
${output_bcu}                   17 85
${input_devicelocatot}          busctl --user get-property bmc.kepler.compute /bmc/kepler/Systems/${SYSTEM_ID}/Memory/Memory_1_01010117 bmc.kepler.Systems.Memory DeviceLocator
${output_devicelocatot}         DIMM000
${input_network_adapter}        busctl --user tree bmc.kepler.network_adapter
${output_network_adapter}       /bmc/kepler/IpmiCmds/30/90/GetMacAddress
${input_bmc_version}            ipmcget -d v
${output_bmc_version}           Product INFO

*** Keywords ***
Clean SSH Response
    [Arguments]    ${response}
    # 移除ANSI颜色代码
    ${clean_rsp}=    Evaluate    re.sub(r'\\x1b\\[[0-9;]*[a-zA-Z]', '', \"\"\"${response}\"\"\")    modules=re
    # 移除Last login信息
    ${clean_rsp}=    Evaluate    re.sub(r'Last login.*?\\n', '', \"\"\"${clean_rsp}\"\"\", flags=re.DOTALL)    modules=re
    # 移除多余空白和换行
    ${clean_rsp}=    Strip String    ${clean_rsp}
    [Return]    ${clean_rsp}

*** Test Cases ***
Get Maca Status cli
    [Documentation]    通过ssh查询maca组件状态
    [Tags]    Get_Maca_Status_1_Via_cli

    # 增加会话稳定性检查
    Wait Until Keyword Succeeds    3x    ${time_out}    Verify SSH Connection    ${ssh}

    # 执行命令并清理内容
    ${rsp}=    Wait Until Keyword Succeeds    3x    ${time_out}    Execute SSH Command    ${ssh}    ${input_maca}
    ${clean_rsp}=    Clean SSH Response    ${rsp}

    # 验证输出
    Should Not Be Empty    ${clean_rsp}    msg=未获取maca组件状态
    #Should Contain    ${clean_rsp}    ${output_maca}    msg=未找到maca组件状态

Get BCU HW Info Via cli
    [Documentation]    通过ssh查询BCU板卡的硬件信息
    [Tags]    Get_BCU_HW_Info_1_Via_cli

    # 增加会话稳定性检查
    Wait Until Keyword Succeeds    3x    ${time_out}    Verify SSH Connection    ${ssh}

    # 执行命令并清理内容 - 为这个命令设置更长超时
    ${rsp}=    Wait Until Keyword Succeeds    3x    ${time_out}    Execute SSH Command    ${ssh}    ${input_bcu}    timeout=60
    ${clean_rsp}=    Clean SSH Response    ${rsp}

    # 验证输出
    Should Not Be Empty    ${clean_rsp}    msg=未获取BCU板卡硬件信息
    Should Contain    ${clean_rsp}    ${output_bcu}    msg=未找到硬件信息  

Get DeviceLocator Info Via cli
    [Documentation]    通过ssh查询内存的DeviceLocator信息
    [Tags]    Get_DeviceLocator_Info_1_Via_cli

    # 增加会话稳定性检查
    Wait Until Keyword Succeeds    3x    ${time_out}    Verify SSH Connection    ${ssh}

    # 执行命令并清理内容
   ${rsp}=    Wait Until Keyword Succeeds    3x    ${time_out}    Execute SSH Command    ${ssh}    ${input_devicelocatot}    timeout=60
    ${clean_rsp}=    Clean SSH Response    ${rsp}

    # 验证输出
    Should Not Be Empty    ${clean_rsp}    msg=未获取内存的DeviceLocator信息
    Should Contain    ${clean_rsp}    ${output_devicelocatot}       msg=未找到内存的DeviceLocator信息

Get NetworkAdapters Tree Via cli
    [Documentation]    通过ssh查询network_adapter组件的资源树
    [Tags]    Get_Network_Adapter_1_Via_cli

    # 增加会话稳定性检查
    Wait Until Keyword Succeeds    3x    ${time_out}    Verify SSH Connection    ${ssh}

    # 执行命令并清理内容
    ${rsp}=    Wait Until Keyword Succeeds    3x    ${time_out}    Execute SSH Command    ${ssh}    ${input_network_adapter}    timeout=60
    ${clean_rsp}=    Clean SSH Response    ${rsp}

    # 验证输出
    Should Not Be Empty    ${clean_rsp}    msg=未获取network_adapter组件的资源树
    Should Contain    ${clean_rsp}    ${output_network_adapter}    msg=未找到network_adapter组件资源树

Get BMC Version Via cli
    [Documentation]    通过ssh查询版本信息
    [Tags]    Get_BMC_Version_1_Via_cli

    # 增加会话稳定性检查
    Wait Until Keyword Succeeds    3x    ${time_out}    Verify SSH Connection    ${ssh}

    # 执行命令并清理内容
    ${rsp}=    Wait Until Keyword Succeeds    3x    ${time_out}    Execute SSH Command    ${ssh}    ${input_bmc_version}
    ${clean_rsp}=    Clean SSH Response    ${rsp}

    # 验证输出
    Should Not Be Empty    ${clean_rsp}    msg=未获取BMC版本信息
    Should Contain    ${clean_rsp}    ${output_bmc_version}       msg=未找到产品信息
